import FixScreen from "./autoFixed";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
import { Constant } from "../constant/Constant";
const { ccclass, property } = cc._decorator;
@ccclass
export default class TouchEvent extends cc.Component {
    @property(cc.Node)
    fatherNode: cc.Node = null;
    /**
     * 当点击触发时
     */
    private touchStart(): void {
        let halfScreen = Constant.SCREEN.width / 2;
        let control = this.fatherNode.getComponent(FixScreen);
        let touchCnt: cc.Touch[] = [];
        this.node.on<cc.Touch>(cc.Node.EventType.TOUCH_START, (touch) => {//点击时
            let cnt = 0;
            let id = touch.getID();
            if (id >= 2) return;
            touchCnt[id] = touch;
            touchCnt.forEach((e) => {
                if (e != null) cnt++;
            })
            switch (cnt) {
                case 1://单点
                    if (touch.getLocationX() > halfScreen) {//右半
                        //  cc.log("右边");
                        control.allToRight();
                    } else {//左半
                        // cc.log("左边");
                        control.allToLeft();
                    }
                    break;
                case 2://两点
                    let fingerTwoSide_one = touchCnt[0].getLocationX() > halfScreen && touchCnt[1].getLocationX() < halfScreen;
                    let fingerTwoSide_two = touchCnt[1].getLocationX() > halfScreen && touchCnt[0].getLocationX() < halfScreen;
                    let fingerOneLeft = touchCnt[0].getLocationX() < halfScreen && touchCnt[1].getLocationX() < halfScreen;
                    if (fingerTwoSide_one || fingerTwoSide_two) {
                        control.splitOver();
                    } else {
                        if (fingerOneLeft) {//两个触点都在左边
                            control.allToLeft();
                        } else {
                            control.allToRight();
                        }
                    }
                    break;
                default: break;//三个以上不作处理
            }
        });
        this.node.on<cc.Touch>(cc.Node.EventType.TOUCH_END, (touch) => {//停止触摸时
            let id = touch.getID();
            touchCnt[id] = null;
            let cnt = 0;
            touchCnt.forEach((e) => {
                if (e != null) cnt++;
            })
            switch (cnt) {
                case 0://手指全部离开屏幕
                    control.autoFixed();
                    break;
                case 1://有一只手指
                    {
                        if (id == 0) {//右边屏幕持续按压
                            if (touchCnt[1].getLocationX() > halfScreen) {//右半
                                //  cc.log("右边");
                                control.allToRight();
                            } else {//左半
                                // cc.log("左边");
                                control.allToLeft();
                            }
                        } else {
                            if (touchCnt[0].getLocationX() > halfScreen) {//右半
                                //  cc.log("右边");
                                control.allToRight();
                            } else {//左半
                                // cc.log("左边");
                                control.allToLeft();
                            }
                        }
                        break;
                    }
            }

        })

    }



    onLoad() {
        this.touchStart();//注册点击事件
    }

    start() {

    }

    // update (dt) {}
}
